﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace ApiDoc
{
    /// <summary>
    /// Manager of Api Documentation for an assembly or group of assemblies.
    /// 
    /// This is the main class for the library and is generally used as the API for finding the documentation types.
    /// </summary>
    public partial class ApiDocumentationManager
    {
        public ApiDocumentationManager()
        {
            // Assemblies we manage and include in our documentation.
            m_assemblies = new List<Assembly>();

            // Get our C# code syntax generator ready for use.
            CodeSyntaxLanguage = "C#";
        }

        /// <summary>
        /// Add an assembly the list of assemblies included in the documentation.
        /// </summary>
        /// <param name="assembly"></param>
        public void AddAssembly(Assembly assembly)
        {
            m_assemblies.Add(assembly);

            // Clear any caches as extension methods etc. will need to be completly regenerated.
            ClearCache();
        }

        
        /// <summary>
        /// Add an assembly the list of assemblies included in the documentation.
        /// </summary>
        /// <param name="assemblies"></param>
        public void AddAssemblyRange(params Assembly[] assemblies)
        {
            AddAssemblyRange(assemblies.ToList());
        }

        /// <summary>
        /// Add an assembly the list of assemblies included in the documentation.
        /// </summary>
        /// <param name="assemblies"></param>
        public void AddAssemblyRange(IEnumerable<Assembly> assemblies)
        {
            foreach (var assembly in assemblies) {
                m_assemblies.Add(assembly);
            }

            // Clear any caches as extension methods etc. will need to be completly regenerated.
            ClearCache();
        }

        /// <summary>
        /// Returns the assemblies being managed.
        /// </summary>
        protected internal IEnumerable<Assembly> GetAssemblies()
        {
            return m_assemblies;
        }


        private List<Assembly> m_assemblies;

        /// <summary>
        /// Clear the caches.
        /// </summary>
        protected void ClearCache()
        {
            // Clear all caches.
            // Please keep this up to date if you add any partial files to the manager with extra caches.
            m_namespaces.Clear();
            m_types.Clear();
            m_constructors.Clear();
            m_methods.Clear();
            m_properties.Clear();
            m_fields.Clear();
            m_events.Clear();
            m_parameters.Clear();
            m_assembliesCache.Clear();
        }
    }
}
